<template>
  <div class="ask-quetion-card">
    <el-row :gutter="20">
      <el-col :span="1" :offset="5">
        <div class="back">
          <i class="el-icon-back" @click="backPage"></i>
        </div>
      </el-col>
      <el-col :span="12">
        <el-card class="box-card">
          <h3>发布提问</h3>
          <div>
            <el-input
              size="medium"
              v-model="articleTitie"
              placeholder="标题，5-30个字，必填"
              maxlength="30"
              show-word-limit
            ></el-input>
          </div>
          <div class="example">
            <div class="thin-font">举例：</div>
            <div class="thin-font">
              大二学生，无法和室友相处，很容易受影响怎么办？
            </div>
            <div class="thin-font">
              情绪低落，很丧，怎么确认我是否患有抑郁症呢？3个月了
            </div>
          </div>
          <div>
            <el-input
              type="textarea"
              :rows="18"
              placeholder="描述内容（40-1000字，必填）建议:【年龄-性别】【大致经过】【内心感受】【经历多长时间】"
              v-model="articleContent"
              maxlength="1000"
              show-word-limit
            >
            </el-input>
          </div>
          <div class="ask-div">
            <el-button type="primary" class="ask-btn" @click="submitArticle"
              >发布提问</el-button
            >
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      articleTitie: "",
      articleContent: "",
    };
  },
  methods: {
    //提交我的提问
    submitArticle() {
      if (this.articleTitie.length < 5) {
        this.$message({
          message: "请输入至少5个字的标题",
          type: "error",
        });
        return;
      }
      if (this.articleContent.length < 40) {
        this.$message({
          message: "请输入至少40个字的内容来描述你的问题",
          type: "error",
        });
        return;
      }
      //增加一个文章
      this.$request.postHttp({
        url: "/article/addArticle",
        data: {
          userid: localStorage.getItem("userId"),
          createtime: new Date(),
          articletitle: this.articleTitie,
          articlecontent: this.articleContent,
          likecount: 0,
          replycount: 0,
        },
        success: (data) => {
          console.log(data);
          if (data.saved) {
            this.$message({
              message: "提交成功",
              type: "success",
            });
            this.$router.replace("/community");
          }
        },
        error: (error) => {
          this.$message({
            message: "提交失败",
            type: "error",
          });
        },
      });
    },
    //返回
    backPage() {
      this.$router.back();
    },
  },
};
</script>

<style scoped>
.ask-quetion-card {
  margin: 70px 0px 0px 0px;
}
.example {
  margin: 20px 0px 20px 0px;
}

.back {
  display: flex;
  align-items: center;
  height: 35px;
  width: 35px;
  font-size: 30px;
  cursor: pointer; /* 添加鼠标悬浮时的样式 */
}

.thin-font {
  font-weight: 300;
}

.ask-div {
  padding: 20px 0px 10px 0px;
}

.ask-btn {
  width: 100%;
}
</style>